package sg.edu.nus.iss.team8s.vmcs.control;
/**
 * 
 * @author Zheng Xiaofeng, Nie Xiaoyi
 *         
 */

import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

import sg.edu.nus.iss.team8s.vmcs.beans.Coin;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreItem;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreObject;
import sg.edu.nus.iss.team8s.vmcs.builder.BuilderFactory;
import sg.edu.nus.iss.team8s.vmcs.builder.MaintenanceBuilder;
import sg.edu.nus.iss.team8s.vmcs.builder.UIBuilder;
import sg.edu.nus.iss.team8s.vmcs.builder.UIDirector;
import sg.edu.nus.iss.team8s.vmcs.cache.CashStore;
import sg.edu.nus.iss.team8s.vmcs.cache.DrinksStore;
import sg.edu.nus.iss.team8s.vmcs.gui.MaintenancePanel;

public class MaintenanceController implements Observer {

	private MaintenancePanel mtnPanel;
	private Label[] lblListCoinQty;
	private Label[] lblListDrinkQty;
	private Label lblTotalValue;
	
	public void update(Observable o, Object arg) {
		StoreItem item = (StoreItem)arg;
		StoreObject obj = (StoreObject)item.getContent();
		String name = obj.getName();
		if(o instanceof DrinksStore){
			displayDrink();
		}
		else if(o instanceof CashStore){
			displayCoin();
		}
	}
	
	private static MaintenanceController instance = null;
	
	private MaintenanceController(){
		DrinksStore.getInstance().addObserver(this);
		CashStore.getInstance().addObserver(this);
	}
	
	public static synchronized MaintenanceController getInstance(){
		if(instance == null){
			instance = new MaintenanceController();
		}
		return instance;
	}
	
	public void setPrice(String brand, int price){
		//refresh all drinks in the screen
		mtnPanel.getContentPane().drinksStoreChange();
	}
	
	public void displayCoin(){
		//refresh all cash items in the screen
		//mtnPanel.getContentPane().cashStoreChange();
		/*if(mtnPanel != null){
			mtnPanel.removeAll();
			buildPanel();
			mtnPanel.validate();
		}*/
		
		if(lblListCoinQty == null || lblTotalValue == null)
			return;
		
		CashStore cashStore = CashStore.getInstance();
		int value = 0;
		for(int i =0;i<cashStore.getStoreSize();i++){
			StoreItem item = cashStore.getItem(i);
			Coin obj = (Coin) item.getContent();
			lblListCoinQty[i].setText(String.valueOf(item.getQuantity()));
			value += item.getQuantity() * obj.getValue();
		}
		lblTotalValue.setText(String.valueOf(value));
	}
	
	public void displayDrink(){
		//refresh all drinks in the screen
		//mtnPanel.getContentPane().drinksStoreChange();
		/*if(mtnPanel != null){
			mtnPanel.removeAll();
			buildPanel();
			mtnPanel.validate();
		}*/
		if(lblListDrinkQty == null)
			return;
		DrinksStore drinkStore = DrinksStore.getInstance();
		
		for(int i =0;i<drinkStore.getStoreSize();i++){
			StoreItem item = drinkStore.getItem(i);

			lblListDrinkQty[i].setText(String.valueOf(item.getQuantity()));
		}
	}
	
	public void displayMaintenancePanel(final SimulationController simController){
		mtnPanel = new MaintenancePanel();
		//mtnPanel.setSize(new Dimension(300,800));
		mtnPanel.setResizable(false);
		mtnPanel.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				simController.setActive(true, '2');
				mtnPanel.setVisible(false);
			}
		});
		buildPanel();
		mtnPanel.display();
	}
	
	private void buildPanel(){
		UIBuilder builder = BuilderFactory.getInstance().getUIBuilder("maintenanceUIBuilder");
		UIDirector uiDir = new UIDirector(builder);
		uiDir.build();
		mtnPanel.add(builder.getResult());
		mtnPanel.setContentPane(builder.getResult());
	}
	
	public void setlblListCoinQty(Label[] lblListCoinQty){
		this.lblListCoinQty = lblListCoinQty;
	}
	
	public void setlblListDrinkQty(Label[] lblListDrinkQty){
		this.lblListDrinkQty = lblListDrinkQty;
	}
	
	public void setlblTotalValue(Label lblTotalValue){
		this.lblTotalValue = lblTotalValue;
	}

}
